/*
 * Eugen Stoian <stoian.e@gmail.com> 2010
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "FixedParameterDelegate.h"

#include <QLineEdit>

FixedParameterDelegate::FixedParameterDelegate(int nameColumn, int valueColumn, QObject* parent)
        :QItemDelegate(parent)
{

    this->_nameColumn = nameColumn;
    this->_valueColumn = valueColumn;
}

void FixedParameterDelegate::paint(QPainter *painter,const QStyleOptionViewItem &option,const QModelIndex &index) const{
    QItemDelegate::paint(painter,option,index);
}

QWidget *FixedParameterDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{
    int c = index.column();

    if(c==_nameColumn){
        QLineEdit *lineEdit1 = new QLineEdit(parent);
        lineEdit1->setReadOnly(true);
        return lineEdit1;
    }
    else if(c==_valueColumn){
        QLineEdit *lineEdit2 = new QLineEdit(parent);
        QRegExp rx("-?(\\d)+(\\.(\\d)+)?(e-?(\\d)+)?");
        QRegExpValidator * validator = new QRegExpValidator(rx,lineEdit2);
        lineEdit2->setValidator(validator);
        connect(lineEdit2,SIGNAL(editingFinished()),this,SLOT(commitAndCloseEditor()));
        return lineEdit2;
    }
    else {
        return QItemDelegate::createEditor(parent,option,index);
    }
}

void FixedParameterDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{
    int c = index.column();
    if(c==_nameColumn){
        QString name = index.model()->data(index,Qt::DisplayRole).toString();
        QLineEdit *lineEdit1 = qobject_cast<QLineEdit *>(editor);
        lineEdit1->setText(name);
    }
    if(c==_valueColumn){
        double value = index.model()->data(index,Qt::DisplayRole).toDouble();
        QLineEdit *lineEdit2 = qobject_cast<QLineEdit *>(editor);
        lineEdit2->setText(QString::number(value));
    }
    else{
        QItemDelegate::setEditorData(editor,index);
    }
}

void FixedParameterDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{
    int c = index.column();
    if(c==_nameColumn){
    }
    if(c==_valueColumn){
        QLineEdit *lineEdit2 = qobject_cast<QLineEdit *>(editor);
        double value = lineEdit2->text().toDouble();
        model->setData(index,value);
    }
    else{
        QItemDelegate::setModelData(editor,model,index);
    }
}

void FixedParameterDelegate::commitAndCloseEditor(){
    QLineEdit *editor = qobject_cast<QLineEdit *>(sender());
    emit commitData(editor);
    emit closeEditor(editor);
}
